<?php

class Entry_model extends MY_Model {

    public function __construct() {
        parent::__construct();

        $this->table = 'jg_entry';
    }

    public function find_by_type($type) {
	$lang = $this->input->cookie('lang')=='en'?'_en':'';
	$select_string = "e.id as id,
		e.album as album,
		e.type as type,
		e.content{$lang} as content,
		e.created_at as created_at,
		e.title{$lang} as title";

	$this->db->select($select_string);
	
        $this->db->from($this->table.' e');

        $this->db->where('e.type', $type);

        $this->db->limit(1);

        $query = $this->db->get();

        return $query->row_object();
    }

    public function simple_total($conditions = array()) {
        $this->db->from($this->table . ' e');
	
        $this->db->join('jg_album a', 'e.album=a.id','left');
        $this->db->join('jg_photo p', 'a.cover=p.id','left');

        if (isset($conditions[0])) {
            foreach ($conditions as $condition) {
                $prefix = strpos($condition[0], '.') === false ? 'e.' : '';
                $this->db->where($prefix . $condition[0], $condition[1]);
                unset($prefix);
            }
        }

        return $this->db->count_all_results();
    }

    public function simple_records($limit = 0, $offset = 0, $conditions = array()) {
	$lang = $this->input->cookie('lang')=='en'?'_en':'';
	$select_string = "e.id as id,
		e.album as album,
		e.type as type,
		e.content{$lang} as content,
		e.created_at as created_at,
		e.title{$lang} as title,
		p.file_name as file_name";

	$this->db->select($select_string);

        $this->db->from($this->table . ' e');

        $this->db->join('jg_album a', 'e.album=a.id','left');
        $this->db->join('jg_photo p', 'a.cover=p.id','left');

        if (isset($conditions[0])) {
            foreach ($conditions as $condition) {
                $prefix = strpos($condition[0], '.') === false ? 'e.' : '';
                $this->db->where($prefix . $condition[0], $condition[1]);
                unset($prefix);
            }
        }

        if ($limit) {
            $this->db->limit($limit, $offset);
        }
        
        $this->db->order_by('e.created_at','desc');

        $query = $this->db->get();

        return $query->result();
    }
    
    public function simple_find($id){
	$lang = $this->input->cookie('lang')=='en'?'_en':'';
	$select_string = "e.id as id,
		e.album as album,
		e.type as type,
		e.content{$lang} as content,
		e.created_at as created_at,
		e.title{$lang} as title";

	$this->db->select($select_string);

        $this->db->from($this->table . ' e');

        $this->db->join('jg_album a', 'e.album=a.id','left');
        $this->db->join('jg_photo p', 'a.cover=p.id','left');
	
	$this->db->where('e.id',$id);
	
	$this->db->limit(1);
	
	$query = $this->db->get();
	
	return $query->row_object();
    }

}
